package com.uduemc.biso.master.backend.validator.impl;

import java.io.IOException;
import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.CollectionUtils;

import com.uduemc.biso.core.extities.center.SysHostType;
import com.uduemc.biso.master.backend.service.HostService;
import com.uduemc.biso.master.backend.validator.HostTypeIdExist;
import com.uduemc.biso.master.config.SpringContextUtils;

public class HostTypeIdExistValid implements ConstraintValidator<HostTypeIdExist, Long> {

	@Override
	public void initialize(HostTypeIdExist constraintAnnotation) {

	}

	@Override
	public boolean isValid(Long value, ConstraintValidatorContext context) {
		if (value == null) {
			return false;
		}
		HostService hostServiceImpl = SpringContextUtils.getBean("hostServiceImpl", HostService.class);
		List<SysHostType> hostType = null;
		try {
			hostType = hostServiceImpl.getHostType();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (!CollectionUtils.isEmpty(hostType)) {
			for (SysHostType sysHostType : hostType) {
				if (sysHostType != null && sysHostType.getId() != null
						&& sysHostType.getId().longValue() == value.longValue()) {
					return true;
				}
			}
		}
		return false;
	}

}
